package com.lvs.zmwz.controller.admin;
import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lvs.zmwz.comm.DictionaryEnum;
import com.lvs.zmwz.entity.SysManager;
import com.lvs.zmwz.service.ISysManagerService;
import com.lvs.zmwz.service.ISysRoleService;
import com.lvs.zmwz.util.IdentityUtil;
import com.lvs.zmwz.util.RandomValidateCodeUtil;
import com.lvs.zmwz.util.ResultView;
import com.lvs.zmwz.util.RsaUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotNull;
import java.util.Date;

@Validated
@RestController
@RequestMapping("/api/admin/login")
public class AdminLoginController {

    @Autowired
    private ISysRoleService iHjRoleService;
    @Autowired
    private ISysManagerService iHjManagerService;

    @PostMapping("/adLogin")
    public ResultView adLogin(@NotNull(message = "账号不能为空") String account,
                              @NotNull(message = "密码不能为空") String pwd,
                              @NotNull(message = "验证码不能为空") String code,
                              HttpServletRequest request){
        //进行登录认证
        QueryWrapper<SysManager> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("m_account",account).or().eq("m_phone",account);
        queryWrapper.eq("m_type",DictionaryEnum.MANAGER_SUPER_ADMIN.getCode());
        queryWrapper.eq("is_del", DictionaryEnum.IS_DEL_N.getCode());
        SysManager hjManager=this.iHjManagerService.getOne(queryWrapper);
        if(hjManager==null){
            return ResultView.error("账号不存在！！！");
        }
        if(hjManager.getMStatus()==DictionaryEnum.IS_ENABLE_Y.getCode()){
            return ResultView.error("账号已被禁用！！！");
        }
        if(!RsaUtil.getRsaPwd(hjManager.getMPwd()).equalsIgnoreCase(pwd)){
            return ResultView.error("密码错误");
        }

        String sysCode = (String)request.getSession().getAttribute(RandomValidateCodeUtil.RANDOMCODEKEY);
        if(!sysCode.equalsIgnoreCase(code)) {
            return ResultView.error("验证码填写错误");
        }
        //进行授权注入sa-token权限框架
        StpUtil.login(hjManager.getManagerId());
        //注入用户session-保持session信息共享-保持session底层代码共享
        StpUtil.getSession().set(hjManager.getManagerId(),hjManager);
        return ResultView.ok(StpUtil.getTokenValue());
    }


    @PostMapping("/insertManager")
    public ResultView insertManager(@NotNull(message = "账号不能为空") String account,
                              @NotNull(message = "密码不能为空") String pwd,
                              @NotNull(message = "用户名不能为空") String mname){
        //进行登录认证
        QueryWrapper<SysManager> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("m_account",account).or().eq("m_phone",account);
        queryWrapper.eq("m_type",DictionaryEnum.MANAGER_SUPER_ADMIN.getCode());
        queryWrapper.eq("is_del", DictionaryEnum.IS_DEL_N.getCode());
        SysManager hjManager=this.iHjManagerService.getOne(queryWrapper);
        if(hjManager!=null){
            return ResultView.error("账号已重复！！！");
        }
        SysManager sysManager=new SysManager();
        sysManager.setManagerId(IdentityUtil.identityId("MA"));
        sysManager.setIsDel(DictionaryEnum.IS_DEL_N.getCode());
        sysManager.setMStatus(DictionaryEnum.IS_ENABLE_N.getCode());
        sysManager.setCtime(new Date());
        sysManager.setMType(DictionaryEnum.MANAGER_SUPER_ADMIN.getCode());
        sysManager.setMAccount(account);
        sysManager.setMName(mname);
        sysManager.setMPwd(RsaUtil.setRsaPwd(pwd));
        this.iHjManagerService.save(sysManager);
        return ResultView.ok();
    }


    /**
     * 生成验证码
     */
    @GetMapping(value = "/getVerify")
    public void getVerify(HttpServletRequest request, HttpServletResponse response) {
        response.setContentType("image/jpeg");//设置相应类型,告诉浏览器输出的内容为图片
        response.setHeader("Pragma", "No-cache");//设置响应头信息，告诉浏览器不要缓存此内容
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expire", 0);
        RandomValidateCodeUtil randomValidateCode = new RandomValidateCodeUtil();
        randomValidateCode.getRandcode(request, response);//输出验证码图片方法
    }

    /**
     * 获取当前用户信息
     */
    @SaCheckLogin
    @GetMapping(value = "/getAccountInfo")
    public ResultView getAccountInfo() {
        return ResultView.ok(StpUtil.getSession().get(StpUtil.getLoginIdAsString()));
    }

}
